package com.zyy.ui.utils;

import com.zyy.ui.dto.ResultDTO;
import com.zyy.ui.enums.EnumForResultCode;
import com.zyy.ui.exception.BaseException;

/**
 * @ClassName: ResultUtil
 * @Description: 获取返回结果，不对外提供code参数，统一用枚举
 * @Author: zyy
 * @Date: 2020/04/25 18:36
 * @Version: 1.0
 */
public class ResultUtil {
    public static <T> ResultDTO<T> getSuccessResult(T data) {
        ResultDTO<T> resultDTO = new ResultDTO<>();
        resultDTO.setCode(EnumForResultCode.SUCCESS.getResultCode());
        resultDTO.setMsg(EnumForResultCode.SUCCESS.getDescribe());
        resultDTO.setData(data);
        return resultDTO;
    }

    public static <T> ResultDTO<T> getFailResult(BaseException be) {
        ResultDTO<T> resultDTO = new ResultDTO<>();
        resultDTO.setCode(be.getErrorCode());
        resultDTO.setMsg(be.getMessage());
        return resultDTO;
    }

    public static <T> ResultDTO<T> getArgsValidFailResult(String message) {
        ResultDTO<T> resultDTO = new ResultDTO<>();
        EnumForResultCode enumForResultCode = EnumForResultCode.ARGS_VALID_FAIL;
        resultDTO.setCode(enumForResultCode.getResultCode());
        resultDTO.setMsg(enumForResultCode.getDescribe() + "：" + message);
        return resultDTO;
    }

    public static <T> ResultDTO<T> getFailResult() {
        ResultDTO<T> resultDTO = new ResultDTO<>();
        resultDTO.setCode(EnumForResultCode.EXCEPTION.getResultCode());
        resultDTO.setMsg(EnumForResultCode.EXCEPTION.getDescribe());
        return resultDTO;
    }
}
